package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.InteractionMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 关注   --  互动消息
 */
@Service("concernInteractService")
public class ConcernInteractServiceImpl implements IInteractMsgService {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        UserInfoEntity user = userInfoService.getById(to.getModelId());
        String remark = String.format("【%s】关注了你", user.getNickname());
        InteractionMessage interactionMessage = new InteractionMessage(SysConstant.InteractionType.USER_CONCERN, to.getModelId(), remark);
        PrivateMessageTO privateMessageTO = new PrivateMessageTO(to.getUserId(), PrivateMessageConstant.MessageType.INTERACTION_MESSAGE,
                interactionMessage, remark);

        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);
        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(to.getUserId())
                        .title(PrivateMessageConstant.MessageType.INTERACTION_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );
    }
}
